1042D - Petya and Array - CodeForces Solution


data structures divide and conquer two pointers *1800

Please click on ads to support us..

C++ Code:

//      بسم الله الرحمن الرحيم
//      All praise is due to ALLAH alone
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define go continue
#define exit return 0
#define ll long long int
#define ull unsigned long long int
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define print_vec(vec) for(int i=0;i<vec.size();i++)cout<<vec[i]<<' ';
#define print_set(st) for(auto it=st.begin();it!=st.end();it++)cout<<*it<<' ';
#define print_pair(vec) for(int i=0;i<vec.size();i++)cout<<vec[i].first<<' '<<vec[i].second<<endl;
#define dbg(num) cerr<< "Line "<<__LINE__ <<": "<< #num <<" = "<<(num)<<endl
//int dx[]={-2, -2, -1, -1,  1,  1,  2,  2};
//int dy[]={-1,  1, -2,  2, -2,  2, -1,  1};
int dx[] = { -1, 0, +1, 0};
int dy[] = {0, +1, 0, -1};
#define mod 1000000007
#define sz 200000
ll arr[sz + 1];
vector<ll>tree[sz * 4 + 4];
void build(int tree_index, int tree_start, int tree_end)
{
	if (tree_start == tree_end)
	{
		tree[tree_index].push_back(arr[tree_start]);
		return;
	}
	int mid = (tree_start + tree_end) / 2;
	build(tree_index * 2, tree_start, mid);
	build(tree_index * 2 + 1, mid + 1, tree_end);
	int i = 0, j = 0;
	while (i < tree[tree_index * 2].size() and j < tree[tree_index * 2 + 1].size())
	{
		ll a = tree[tree_index * 2][i];
		ll b = tree[tree_index * 2 + 1][j];
		if (a <= b)
			tree[tree_index].push_back(a), i++;
		else tree[tree_index].push_back(b), j++;
	}
	while (i < tree[tree_index * 2].size())
		tree[tree_index].push_back(tree[tree_index * 2][i]), i++;
	while (j < tree[tree_index * 2 + 1].size())
		tree[tree_index].push_back(tree[tree_index * 2 + 1][j]), j++;
}
int query(int tree_index, int tree_start, int tree_end, int l, int r, ll val)
{
	if (tree_start > r or tree_end < l)
		return 0;
	if (tree_start >= l and tree_end <= r)
	{
		int pos = lower_bound(tree[tree_index].begin(), tree[tree_index].end(), val) - tree[tree_index].begin();
		return pos;
	}
	int mid = (tree_start + tree_end) / 2;
	int left = query(tree_index * 2, tree_start, mid, l, r, val);
	int right = query(tree_index * 2 + 1, mid + 1, tree_end, l, r, val);
	return left + right;
}
int main()
{
	fast;
	ll n, t;
	cin >> n >> t;
	for (int i = 1; i <= n; i++)
	{
		ll a;
		cin >> a;
		if (i == 1)
			arr[i] = a;
		else arr[i] = arr[i - 1] + a;
	}
	build(1, 1, n);
	ll ans = 0;
	ans += query(1, 1, n, 1, n, t);
	for (int i = 1; i < n; i++)
	{
		ll cost = arr[i] + t;
		ans += query(1, 1, n, i + 1, n, cost);
	}
	cout << ans << endl;
//.............................................. ٱلْحَمْدُ لِلَّٰ......................................
}


Comments

Submit
0 Comments
More Questions

152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set